home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug191 / soundctl.c < prev    next >
Text File  |  1986-05-25  |  8KB  |  272 lines

  1.  
  2. /*  SOUNDCTL.C                          Last update: 27 Feb 86  */
  3.  
  4. /* ------------------------------------------------------------ */
  5. /*      This is a portion of the SOUND EFFECTS LIBRARY.         */
  6. /*                                                              */
  7. /*      Copyright (C) 1986 by Paul Canniff.                     */
  8. /*      All rights reserved.                                    */
  9. /*                                                              */
  10. /*      This library has been placed into the public domain     */
  11. /*      by the author.  Use is granted for non-commercial       */
  12. /*      pusposes, or as an IMBEDDED PORTION of a commercial     */
  13. /*      product.                                                */
  14. /*                                                              */
  15. /*      Paul Canniff                                            */
  16. /*      PO Box 1056                                             */
  17. /*      Marlton, NJ 08053                                       */
  18. /*                                                              */
  19. /*      CompuServe ID: 73047,3715                               */
  20. /*                                                              */
  21. /* ------------------------------------------------------------ */
  22.  
  23.  
  24.  
  25. #define DEBUG 0
  26.  
  27. #include <stdio.h>
  28. #include "sound.h"
  29.  
  30. #if C_AZTEC
  31. #include <model.h>
  32. #endif
  33.  
  34. #if C_ECOSOFT
  35. #include <dos.h>
  36. #endif
  37.  
  38. #if C_LATTICE
  39. #include <dos.h>
  40. #endif
  41.  
  42. #define HTMRINT 0x8
  43. #define UTMRINT 0x1c
  44. #define BRKINT 0x23
  45. #define SPAREINT 0x60
  46. #define OLDINT 0x61
  47.  
  48. typedef int (*PFI)();
  49.  
  50. static long old_usr_tmr_int=0;          /* Old user timer int vector */
  51. static long old_hw_tmr_int=0;           /* Old hardware timer int vector */
  52. static long old_break_int=0;            /* Old Ctrl-break int vector */
  53. static int inited = 0;                  /* Current system state */
  54.  
  55. /* -------------------------------------------------------------------- *
  56.  *      MUS_OPEN                Open music routines.                    *
  57.  *      int mus_open(bm)                                                *
  58.  *      int bm;                                                         *
  59.  *                                                                      *                                                                      
  60.  *      Sets interrupt vectors, stores needed sgement values.           *
  61.  *                                                                      *
  62.  *      bm = break mode.  If 1, a Ctrl-break handler is set up, which   *
  63.  *              will (a) shut down the music stuff, then (b) call       *
  64.  *              whatever break handler was previously in effect.        *
  65.  *                                                                      *
  66.  *      Returns:         0  Open OK                                     *
  67.  *                      -1  If interrupt at 0x60 already in use         *
  68.  *                      -2  If already "open"                           *
  69.  * -------------------------------------------------------------------- */
  70.  
  71. static int current_mode = 1;
  72.  
  73. int sound_init(mode,ibh)
  74. int mode,ibh;
  75. {
  76.     extern long install(), snd_giv();
  77.     extern int snd_irh(), snd_brk();
  78.     long vec;
  79.  
  80. #if DEBUG
  81.     printf("In sound init, mode = %d\n",mode);
  82. #endif
  83.  
  84.     if (mode < 1 || mode > 5) return -1;        /* Check parameter range */
  85.  
  86.     sound_done();                               /* Clean up if necessary */
  87.  
  88.     if (snd_giv(SPAREINT) != 0)             /* Is "spare int" in use? */
  89.         return -1;
  90.  
  91.     if (snd_giv(OLDINT) != 0)               /* Is "old int" in use? */
  92.         return -1;
  93.  
  94.     if (ibh) old_break_int = install(BRKINT,snd_brk);                        
  95.      else    old_break_int = 0;
  96.  
  97.  
  98.         /* NOTE:  Modes 3 and 5 not currently in operation! */
  99.         /*        Treated as modes 2 and 4 respectively     */
  100.  
  101.     if (mode == 3 || mode == 5)
  102.         --mode;
  103.  
  104.     if (mode == 2 || mode == 4)
  105.     {
  106.         old_usr_tmr_int = snd_giv(UTMRINT);
  107. #if DEBUG
  108.         printf("Placing old user timer vector %8lx at %02x\n",
  109.             old_usr_tmr_int,OLDINT);
  110. #endif
  111.         snd_siv(OLDINT,old_usr_tmr_int);
  112.         install(UTMRINT,snd_irh);
  113.         old_hw_tmr_int = 0;
  114.     }
  115.  
  116. #if DEBUG
  117.     printf("INT 0x%2x = %08lx\n",HTMRINT,snd_giv(HTMRINT));
  118.     printf("INT 0x%2x = %08lx\n",UTMRINT,snd_giv(UTMRINT));
  119.     printf("INT 0x%2x = %08lx\n",BRKINT,snd_giv(BRKINT));
  120.     printf("INT 0x%2x = %08lx\n",SPAREINT,snd_giv(SPAREINT));
  121.     printf("INT 0x%2x = %08lx\n",OLDINT,snd_giv(OLDINT));
  122. #endif
  123.  
  124.     inited = 1;
  125.     current_mode = mode;
  126.  
  127. #if DEBUG
  128.     printf("Init done, mode set to %d\n",current_mode);
  129. #endif
  130.  
  131.     return 0;
  132. }
  133.  
  134.  
  135. /* -------------------------------------------------------------------- *
  136.  *      MUS_CLOSE               Close music routines.                   *
  137.  *      int mus_close()                                                 *
  138.  *                                                                      *                                                                      
  139.  *      Resets interrupt vectors.                                       *
  140.  *                                                                      *
  141.  *      Returns:         0  Close OK                                    *
  142.  *                      -2  If not "open"                               *
  143.  * -------------------------------------------------------------------- */
  144.  
  145. sound_done()
  146. {
  147. #if DEBUG
  148.     printf("In sound_done, hushing speaker ...\n");
  149. #endif
  150.  
  151.     quiet();
  152.  
  153.     if (old_break_int != 0)
  154.     {
  155. #if DEBUG
  156.         printf("  Restoring BRK\n");
  157. #endif
  158.         snd_siv(BRKINT,old_break_int);
  159.         old_break_int = 0;
  160.     }
  161.  
  162.     if (old_usr_tmr_int != 0)
  163.     {
  164. #if DEBUG
  165.         printf("  Restoring UTMR\n");
  166. #endif
  167.         snd_siv(UTMRINT,old_usr_tmr_int);
  168.         old_usr_tmr_int = 0;
  169.     }
  170.  
  171.     if (old_hw_tmr_int != 0)
  172.     {
  173. #if DEBUG
  174.         printf("  Restoring HTMR\n");
  175. #endif
  176.         snd_siv(HTMRINT,old_hw_tmr_int);
  177.         old_hw_tmr_int = 0;
  178.     }
  179.  
  180. #if DEBUG
  181.     printf("  Clearing Spare & Old Timer\n");
  182. #endif
  183.     snd_siv(SPAREINT,0L);
  184.     snd_siv(OLDINT,0L);
  185.  
  186.     inited = 0;
  187.  
  188.     return 0;
  189. }
  190.  
  191.  
  192. int sound_mode()
  193. {
  194.     return current_mode;
  195. }
  196.  
  197.  
  198. #if C_AZTEC
  199. #define COMMON_INSTALL 1
  200. typedef unsigned SEGREGS[4]; 
  201. #define SEGADDR(x) (x)
  202. #define XCSEG(x) (x[0])
  203. #ifdef _LARGECODE
  204. #define LPROG 1
  205. #else
  206. #define LPROG 0
  207. #endif
  208. #endif
  209.  
  210. #if C_LATTICE
  211. #define COMMON_INSTALL 1
  212. typedef struct SREGS SEGREGS; 
  213. #define SEGADDR(x) (&(x))
  214. #define XCSEG(x) ((x).cs)
  215. #ifdef I8086P
  216. #define LPROG 1
  217. #endif
  218. #ifdef I8086L
  219. #define LPROG 1
  220. #endif
  221. #ifndef LPROG
  222. #define LPROG 0
  223. #endif
  224. #endif
  225.  
  226. #if C_ECOSOFT
  227. #define COMMON_INSTALL 1
  228. #define LPROG 0
  229. typedef struct SREG SEGREGS;
  230. #define SEGADDR(x) (&(x))
  231. #define XCSEG(x) ((x).cs)
  232. #endif
  233.  
  234.  
  235. #if COMMON_INSTALL
  236.  
  237. static long install(v,fp)
  238. int v;
  239. PFI fp;
  240. {
  241.     long oldvec, vec, snd_giv();
  242.     SEGREGS sr;
  243.  
  244.     oldvec = snd_giv(v);
  245. #if DEBUG
  246.     printf("Install %02x  Old = %08lx ",v,oldvec);
  247. #endif
  248.  
  249. #if LPROG
  250.     snd_siv(v,fp);
  251. #if DEBUG
  252.     printf(" Long New = %08lx \n\n",fp);
  253. #endif
  254. #else
  255.     segread(SEGADDR(sr));
  256.     vec = ((long) XCSEG(sr) << 16) | (long) ((unsigned) fp & 0xffff);
  257. #if DEBUG
  258.     printf(" CS = %04x   Short New = %08lx \n\n",XCSEG(sr),vec);
  259. #endif
  260.     snd_siv(v,vec);
  261. #endif
  262.  
  263. #if DEBUG
  264.     printf("done\n");
  265. #endif
  266.     return oldvec;
  267. }
  268.  
  269. #endif
  270.  
  271.  
  272.